package Eyjafjalla.effect;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.MathUtils;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.vfx.AbstractGameEffect;

public class FireBreathParticleEffect extends AbstractGameEffect {
    private TextureAtlas.AtlasRegion img;
    private static final float GRAVITY = 180F;
    private float vY;
    float x,y;

    public FireBreathParticleEffect(float x,float y){
        int roll = MathUtils.random(0,2);
        if (roll == 0) {
            this.img = ImageMaster.FLAME_1;
        } else if (roll == 1) {
            this.img = ImageMaster.FLAME_2;
        } else {
            this.img = ImageMaster.FLAME_3;
        }

        this.duration = MathUtils.random(0.6F,1.0F);
        this.color = new Color(MathUtils.random(0.8F, 1.0F), MathUtils.random(0.3F, 0.8F), MathUtils.random(0.1F, 0.2F), 0.0F);
        this.rotation = MathUtils.random(-10.0F, 10.0F);
        this.scale = MathUtils.random(3.6F, 4.8F) * Settings.scale;
        this.vY = MathUtils.random(100.0F, 300.0F) * Settings.scale;
        this.x = x;
        this.y = y;
    }

    @Override
    public void update() {
        this.vY += GRAVITY / this.scale * Gdx.graphics.getDeltaTime();
        this.y += this.vY * Gdx.graphics.getDeltaTime();
        if (this.scale > 0.3F * Settings.scale) {
            this.scale -= Gdx.graphics.getDeltaTime() * 2.0F;
        }

        if (1F - this.duration < 0.1F) {
            this.color.a = Interpolation.fade.apply(0.0F, 0.8F, (1F - this.duration) * 10.0F);
        } else {
            this.color.a = Interpolation.pow2Out.apply(0.0F, 0.8F, this.duration);
        }

        this.duration -= Gdx.graphics.getDeltaTime();
        if (this.duration < 0.0F) {
            this.isDone = true;
        }
    }

    @Override
    public void render(SpriteBatch sb) {
        sb.setBlendFunction(770, 1);
        sb.setColor(this.color);
        sb.draw(this.img, this.x, this.y, 0F, 0F, (float)this.img.packedWidth, (float)this.img.packedHeight, this.scale, Settings.scale, this.rotation);
        sb.setBlendFunction(770, 771);
    }

    @Override
    public void dispose() {

    }
}
